import Layout from "@/layout/index.vue";
import router from "@/router/index.js";
import {HOME_URL} from "@/config/index.js";

// 扁平化菜单树，并加入基本配置
export function generateFlat(data) {
  const icon = ["CirclePlus", "Position", "ChatDotSquare", "Refresh", "Compass",
    "WarningFilled", "CloseBold", "SwitchButton"];
  const subIcon = ["Message", "Operation", "Open", "ZoomOut", "Filter",
    "Finished", "MoreFilled", "HomeFilled", "Histogram", "Promotion", "Tools"];
  data.forEach((item, index) => {
    // 一级路由
    item.parentId = null;
    item.type = 1;
    item.name = item.SysMenuItems[0].linkurl.split("/")[1];
    item.component = "";
    item.title = item.pro_name; // 标题
    item.path = "/" + item.SysMenuItems[0].linkurl.split("/")[1]; // 路由
    item.icon = icon[index]; // 图标
    item.isHide = false; // 代表路由在菜单中是否隐藏，是否隐藏[0隐藏，1显示]
    item.isLink = item.SysMenuItems.length > 0 ? item.SysMenuItems[0].linkurl : ""; // 是否外链[有值则是外链]
    item.isKeepAlive = false; // 是否缓存路由数据[0是，1否]
    item.isFull = false; // 是否缓存全屏[0是，1否]
    item.isAffix = false;// 是否缓存固定路由[0是，1否]
    // 子路由
    if (item.SysMenuItems.length > 0) {
      item.SysMenuItems.forEach((sub, i) => {
        sub.parentId = item.sort_id;
        sub.type = 2;
        sub.name = sub.linkurl.split("/")[2];
        sub.component = sub.linkurl.substr(1) + "/index";
        sub.title = sub.pro_name; // 标题
        sub.path = sub.linkurl; // 路由
        sub.icon = subIcon[i]; // 图标
        sub.isHide = false; // 代表路由在菜单中是否隐藏，是否隐藏[0隐藏，1显示]
        sub.isLink = "";
        sub.isKeepAlive = true; // 是否缓存路由数据[0是，1否]
        sub.isFull = false; // 是否缓存全屏[0是，1否]
        sub.isAffix = false;// 是否缓存固定路由[0是，1否]
      });
    }
  });
  const arr = [];
  data.forEach(item => {
    let child = item.SysMenuItems;
    arr.push(item);
    child.forEach(c => {
      arr.push(c);
    });
  });

  return arr;
}

// 递归函数用于生成路由配置，登录的时候也需要调用一次。
export function generateRoutes(data, parentId) {
  // 首先把你需要动态路由的组件地址全部获取[vue2中可以直接用拼接的方式，但是vue3中必须用这种方式]
  let modules = import.meta.glob("@/views/**/*.vue");
  const routeList = [];
  for (let i = 0; i < data.length; i++) {
    if (data[i] && !router.hasRoute(data[i].path)) {
      if (data[i].parentId === parentId) {
        const componentTemplate = data[i].component;
        const route = {
          path: `${data[i].path}`,
          name: `${data[i].name}`,
          // 这里modules[`/src/views/${componentTemplate}.vue`] 一定要用绝对定位
          component: data[i]?.component ? modules[`/src/views/${componentTemplate}.vue`] : Layout,
          meta: {
            title: data[i].title,
            icon: data[i].icon,
            isHide: data[i].isHide,
            isKeepAlive: data[i].isKeepAlive,
            isLink: data[i].isLink,
            isFull: data[i].isFull,
            isAffix: data[i].isAffix,
            redirect: data[i].redirect,
          },
        };
        if (data[i].type === 1) {
          route.redirect = `${data[i]?.isLink}` || HOME_URL;
        }
        // 递归处理子节点
        const children = generateRoutes(data, data[i].sort_id);
        if (children.length > 0) {
          route.children = children;
        }
        routeList.push(route);
      }
    }
  }
  return routeList;
}

/**
 * 初始化动态路由[用于生成扁平化一级路由，将后端一级路由数据转化为前端router格式的一级路由]
 */
export function generateFlattenRoutes(data) {
  // 首先把你需要动态路由的组件地址全部获取[vue2中可以直接用拼接的方式，但是vue3中必须用这种方式]
  let modules = import.meta.glob("@/views/**/*.vue");
  const routes = [];
  for (let i = 0; i < data.length; i++) {
    const componentTemplate = data[i].component;
    const route = {
      path: `${data[i].path}`,
      name: `${data[i].name}`,
      component: data[i]?.component ? modules[`/src/views/${componentTemplate}.vue`] : Layout,
      meta: {
        parentId: data[i].parentId,
        title: data[i].title,
        icon: data[i].icon,
        isHide: data[i].isHide,
        isKeepAlive: data[i].isKeepAlive,
        isLink: data[i].isLink,
        isFull: data[i].isFull,
        isAffix: data[i].isAffix,
        redirect: data[i].redirect,
      }
    };
    if (data[i].type === 1) {
      route.redirect = `${data[i]?.isLink}` || HOME_URL;
    }
    routes.push(route);
  }
  return routes;
}
